<?php
namespace Home\Controller;
use Think\Controller;
class CardController extends Controller {
	
	/**
	 * 用户的名片页面
	 */
	public function index($id=null) {
		$userModel = M('User');
		$where=array();
		if($id!=null){
			$where['id']=$id;
		}
		$userInfo = $userModel->where($where) -> find();
		$this -> assign('info', $userInfo);
		$this -> display();
	}

	function qrurl(){
		include 'phpqrcode/phpqrcode.php';
		$dao = M('User');
		$id=intval($_GET['id']);
		$info=$dao->find($id);
		if(empty($info)){
			exit('0');
		}
		$value = 'http://card.wttai.com/card/'.$id; //二维码内容
		$errorCorrectionLevel = 'L';
		//容错级别
		$matrixPointSize = 6;
		//生成图片大小
		$logo =empty($info['head_image'])?false:$info['head_image'];
		$QR = $logo?'temp_qrcode.png':false;
		//生成二维码图片
		\QRcode::png($value,$QR, $errorCorrectionLevel, $matrixPointSize, 2);
		
		//已经生成的原始二维码图
		if ($logo !== false) {
			$QR = imagecreatefromstring(file_get_contents($QR));
			$logo = imagecreatefromstring(file_get_contents($logo));
			$QR_width = imagesx($QR);
			//二维码图片宽度
			$QR_height = imagesy($QR);
			//二维码图片高度
			$logo_width = imagesx($logo);
			//logo图片宽度
			$logo_height = imagesy($logo);
			//logo图片高度
			$logo_qr_width = $QR_width / 5;
			$scale = $logo_width / $logo_qr_width;
			$logo_qr_height = $logo_height / $scale;
			$from_width = ($QR_width - $logo_qr_width) / 2;
			//重新组合图片并调整大小
			imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
			//输出图片
			Header("Content-type: image/png");
			ImagePng($QR);
		}
	}

	/**
	 * 微信扫码的名片信息 H5的url
	 */
	function qrvcf() {
		include 'phpqrcode/phpqrcode.php';
		$dao = M('User');
		$id=intval($_GET['id']);
		$info=$dao->find($id);
		if(empty($info)){
			exit('0');
		}
		$vcard = array(
			'vname' => $info['name'].$info['nickname'], 
			'vtel' => $info['telephone'], 
			'vemail' => $info['email'], 
			'vaddress' => $info['address'],
			);

		$chl = "BEGIN:VCARD\nVERSION:3.0" . //vcard头信息
				"\nFN:" . $vcard['vname'] . 
				"\nTEL:" . $vcard['vtel'] . 
				"\nEMAIL:" . $vcard['vemail'] . 
				"\nADR;WORK:" . $vcard['vaddress'];
		if($info['company']){
			$chl.="\nORG:" . $info['company'];
		}
		if($info['position']){
			$chl.="\nTITLE:" . $info['position'];
		}
		if($info['department']){
			$chl.="\nROLE:" . $info['department'];
		}
		if($info['homepage_url']){
			$chl.="\nURL:" . $info['homepage_url'];
		}
		if($info['nickname']){
			$chl.="\nNICKNAME:" . $info['nickname'];
		}
		if($info['moblie-phone']){
			$chl.="\nTEL;CELL;VOICE:" . $info['moblie-phone'];
		}
		if($info['head_image']){
			$chl.="\nPHOTO;VALUE=uri:http://card.wttai.com/pic/" . $info['head_image'];
		}
				
		$chl.="\nEND:VCARD";
		//vcard尾信息
		$errorCorrectionLevel = 'L';
		//容错级别
		$matrixPointSize = 6;
		//生成图片大小
		//生成二维码图片
		$logo =empty($info['head_image'])?false:$info['head_image'];
		$QR = $logo?'temp_qrcode.png':false;
		//生成二维码图片
		\QRcode::png($chl,$QR, $errorCorrectionLevel, $matrixPointSize, 2);
		
		//已经生成的原始二维码图
		if ($logo !== false) {
			$QR = imagecreatefromstring(file_get_contents($QR));
			$logo = imagecreatefromstring(file_get_contents($logo));
			$QR_width = imagesx($QR);
			//二维码图片宽度
			$QR_height = imagesy($QR);
			//二维码图片高度
			$logo_width = imagesx($logo);
			//logo图片宽度
			$logo_height = imagesy($logo);
			//logo图片高度
			$logo_qr_width = $QR_width / 5;
			$scale = $logo_width / $logo_qr_width;
			$logo_qr_height = $logo_height / $scale;
			$from_width = ($QR_width - $logo_qr_width) / 2;
			//重新组合图片并调整大小
			imagecopyresampled($QR, $logo, $from_width, $from_width, 0, 0, $logo_qr_width, $logo_qr_height, $logo_width, $logo_height);
			//输出图片
			Header("Content-type: image/png");
			ImagePng($QR);
		}
	}

	/**
	 * 发送vcf文件
	 */
	public function vcf($id = null) {
		if ($id == null) {
			$this -> error('参数错误');
		}
		import('@Com.VCardIFL');
		$data = M('User') -> find($id);
		$arData = array();
		$arData['fileName'] = $data['name'] . '_' . $data['mobile'];
		$arData['vcard_f_name'] = $data['name'];
		$arData['vcard_c_mobile'] = $data['mobile'];
		$arData['vcard_compan'] = $data['compan'];
		$arData['vcard_birtda'] = $data['birtday'];
		$arData['vcard_h_mail'] = $data['email'];
		$arData['vcard_h_zip'] = $data['zip'];
		$arData['vcard_h_phon'] = $data['tel'];
		$arData['vcard_h_addr'] = $data['addr'];
		$card = new \Home\Com\VCardIFL($arData);
		$card -> createVcard();
		$card -> DownloadVcard();
	}

}
